package com.competition.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.competition.dao.EquipmentLeasingOrderDao;
import com.competition.entity.EquipmentAudit;
import com.competition.entity.EquipmentLeasing;
import com.competition.entity.EquipmentLeasingOrder;
import com.competition.mapper.EquipmentLeasingOrderMapper;
import com.competition.resp.ResponseResult;
import com.competition.service.EquipmentAuditService;
import com.competition.service.EquipmentLeasingOrderService;
import com.competition.service.EquipmentLeasingService;
import com.competition.utils.uuid.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/equipmentOrder")
public class EquipmentLeasingOrderController {
    @Autowired
    private EquipmentLeasingOrderService equipmentLeasingOrderService;

    @Autowired
    private EquipmentAuditService equipmentAuditService;

    @Autowired
    private EquipmentLeasingService equipmentLeasingService;
    @Autowired
    private EquipmentLeasingOrderMapper equipmentLeasingOrderMapper;

    @GetMapping("/queryEquipmentOrderList")
    public ResponseResult<Object> queryEquipmentOrderList(@RequestParam HashMap<String,Object> params){//获取后台的租赁列表
        if(params.get("page")==null||params.get("pageSize")==null){
            return new ResponseResult<Object>(400,"参数错误",null);
        }
        int page = Integer.parseInt(params.get("page").toString());
        int pageSize = Integer.parseInt(params.get("pageSize").toString());
        HashMap<String, Object> reqMap = params;
        reqMap.put("limit",pageSize);
        reqMap.put("offset",(page - 1) * pageSize);
        List<EquipmentLeasingOrder> equipmentOrderList =   equipmentLeasingOrderService.queryEquipmentOrderList(reqMap);
        long count = equipmentLeasingOrderService.count();
        HashMap<String,Object> resMap = new HashMap<>();
        resMap.put("equipmentOrderList",equipmentOrderList);
        resMap.put("total",count);
        return   new ResponseResult<Object>(200,"获取租赁列表成功",resMap);

    }

    @PutMapping("/modifyEquipmentOrderStutus")
    public ResponseResult<Object> modifyEquipmentOrderStutus(@RequestBody HashMap<String,Object> params) {//修改租赁订单状态
        if(params.get("equipmentOrderId")==null || params.get("equipmentOrderStatus")==null){
            return new ResponseResult<Object>(400,"参数错误",null);
        }
        UpdateWrapper<EquipmentLeasingOrder> objectUpdateWrapper = new UpdateWrapper<>();
        objectUpdateWrapper.eq("equipment_order_id",params.get("equipmentOrderId"));
        objectUpdateWrapper.set("equipment_order_status",params.get("equipmentOrderStatus"));
        equipmentLeasingOrderService.update(objectUpdateWrapper);
        return new ResponseResult<Object>(200, "修改租赁订单状态成功", null);
    }
    @PostMapping("/addOrder")
    @Transactional(isolation = Isolation.READ_COMMITTED,rollbackFor = {Exception.class,RuntimeException.class})
    public ResponseResult addOrder(@RequestBody HashMap<String,Object> params){
        if(params.get("equipmentId")==null || params.get("auditCount")==null||params.get("openid")==null || params.get("equipmentInventory")==null){
            return new ResponseResult<Object>(400,"参数错误",null);
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("equipment_id",(String) params.get("equipmentId"));
        EquipmentLeasing one = equipmentLeasingService.getOne(queryWrapper);
        if (one.getEquipmentInventory()< (Integer) params.get("auditCount")){
            return new ResponseResult(400,"添加异常",null);
        }

        try {
            //添加记录
            equipmentAuditService.save(new EquipmentAudit(UUIDUtil.getUUID(),(String) params.get("equipmentId"),"审核中","无",(Integer) params.get("auditCount"),
                    (String) params.get("openid"),null,null));

            //修改数据
            UpdateWrapper updateWrapper = new UpdateWrapper();
            updateWrapper.set("equipment_inventory",(Integer) params.get("equipmentInventory"));
            updateWrapper.eq("equipment_id",(String) params.get("equipmentId"));
            equipmentLeasingService.update(updateWrapper);

            //生成订单
            equipmentLeasingOrderService.save(new EquipmentLeasingOrder(UUIDUtil.getUUID(),(String) params.get("equipmentId"),
                    (Integer) params.get("equipmentInventory"),"未归还",(String) params.get("openid"),null,null));

            return new ResponseResult(200,"成功生成订单",null);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
//            return new ResponseResult(400,"生成失败",null);
        }
    }
    @GetMapping("/getByStatusList")
    public ResponseResult getByStatusList(@RequestParam("openId")String openId,@RequestParam("through")String through,@RequestParam("current")int current){
        List<EquipmentLeasingOrderDao> selectAllByUserIdAndAuditStatusAndAuditStatus = equipmentLeasingOrderMapper.getSelectAllByUserIdAndAuditStatusAndAuditStatus(openId, through, current);

        return new ResponseResult(200,"获取成功",selectAllByUserIdAndAuditStatusAndAuditStatus);
    }
}
